package com.winit.openapi.tools.validator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

/**
 * winit产品相关接口校验类
 * 
 * @version
 *          <pre>
 * Author	Version		Date		Changes
 * bing.gu 	1.0  		2017年6月26日 	Created
 *
 * </pre>
 * @since 1.
 */
public class WinitToolsProductFieldValidator {

    /**
     * 产品推荐入参校验
     * 
     * @param json
     */
    public static void recommendRequesParamsValidate(JSONObject json) {
        // 仓库编码
        ValidateUtil.validMaxLength(json, "warehouseCode", 128, true);
       
        // 校验类型(EGD_ONLY:EGD校验,EBAY_ONLY:只校验是否ebay订单,USABLE_P_ONLY:获取可用产品列表,ALL:前面三种功能的集合)
        ValidateUtil.typeValidate(json, "validationType", new String[] { "EGD_ONLY", "EBAY_ONLY", "USABLE_P_ONLY",
                "ALL" }, true);
        if ("EGD_ONLY".equals(json.get("validationType").toString())
                || "EBAY_ONLY".equals(json.get("validationType").toString())) {
            // 校验类型为EGD的情况下,sellerID不能为空
            ValidateUtil.validMaxLength(json, "sellerID", 128, true);
            //校验增值服务
            if (json.containsKey("VASList")) {
                JSONArray vasArray = json.getJSONArray("VASList");
                for (Object vas : vasArray) {
                    JSONObject obj = (JSONObject) vas;
                    ValidateUtil.validNotNull(obj, "serviceCode");
                    //ValidateUtil.validNotNull(obj, "attrList");
                   // ValidateUtil.validList(obj, "attrList", true);
                    JSONArray vasAttrArray = obj.getJSONArray("attrList");
                    if(null==vasAttrArray||vasAttrArray.size()==0){
                        continue;
                    }
                    for (Object attr : vasAttrArray) {
                        JSONObject attrObj = (JSONObject) attr;
                        ValidateUtil.validNotNull(attrObj, "attrCode");
                        ValidateUtil.validNotNull(attrObj, "attrValue");
                    }
                }
            }
        }
        
        if (!"USABLE_P_ONLY".equals(json.get("validationType").toString())) {
            // winit产品编码
            ValidateUtil.validMaxLength(json, "winitProductCode", 128, true);
        }
        // 地址信息校验
        recommendRequesParamsAddressValidate(json);
        // 包裹信息校验
        recommendRequesParamsParcelsValidate(json);
    }

    /**
     * EGD校验参数
     * 
     * @param json
     */
    public static void egdValidateParamsValidate(JSONObject json) {
        ValidateUtil.validList(json, "outboundOrderNos", true);
        JSONArray parcels = json.getJSONArray("outboundOrderNos");
        if (parcels.size() == 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "parcels");
        } else if (parcels.size() > 10){
            throw new ApiException(ErrorCode.EGD_VALIDATE_COUNT_OF_ORDER_MAX, 10);
        }
    }

    /**
     * 产品推荐入参:收件人地址校验
     * 
     * @param json
     */
    private static void recommendRequesParamsAddressValidate(JSONObject json) {
        // 收件人地址
        ValidateUtil.validNotNull(json, "receiverAddress");
        JSONObject address = (JSONObject) json.get("receiverAddress");
        // 收件人名称
        ValidateUtil.validMaxLength(address, "name", 128, true);
        // 收件人电话
        ValidateUtil.validMaxLength(address, "phone", 128, false);
        // 收件人email
        ValidateUtil.validMaxLength(address, "email", 128, false);
        // 收件人地址一
        ValidateUtil.validMaxLength(address, "addressLine1", 128, true);
        // 收件人地址二
        ValidateUtil.validMaxLength(address, "addressLine2", 128, false);
        // 收件人城市
        ValidateUtil.validMaxLength(address, "city", 128, true);
        // 收件人州
        ValidateUtil.validMaxLength(address, "state", 128, false);
        // 收件人国家
        ValidateUtil.validMaxLength(address, "country", 128, true);
        // 收件人邮编
        ValidateUtil.validMaxLength(address, "postCode", 128, true);
        // TODO 收件人门牌号
    }

    /**
     * 产品推荐入参:包裹数据校验
     * 
     * @param json
     */
    private static void recommendRequesParamsParcelsValidate(JSONObject json) {
        // 包裹信息
        ValidateUtil.validNotNull(json, "parcels");
        ValidateUtil.validList(json, "parcels", true);
        JSONArray parcels = json.getJSONArray("parcels");
        if (parcels.size() == 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "parcels");
        }
        for (Object obj : parcels) {
            JSONObject parcel = (JSONObject) obj;
            recommendRequesParamsMerchandisesValidate(parcel,
                    null == json.get("validationType") ? "" : json.get("validationType").toString());
        }
    }

    /**
     * 产品推荐入参:商品信息校验
     * 
     * @param json
     */
    private static void recommendRequesParamsMerchandisesValidate(JSONObject json, String validationType) {
        ValidateUtil.validNotNull(json, "merchandiseInfos");
        ValidateUtil.validList(json, "merchandiseInfos", true);
        JSONArray merchandises = json.getJSONArray("merchandiseInfos");
        // 商品信息
        if (merchandises.size() == 0) {
            throw new ApiException(ErrorCode.FIELD_NOT_NULL, "merchandiseInfos");
        }
        for (Object obj : merchandises) {
            JSONObject merchandise = (JSONObject) obj;
            ValidateUtil.validMaxLength(merchandise, "SKU", 128, true);
            ValidateUtil.validMaxLength(merchandise, "specification", 128, false);
            ValidateUtil.validMaxLength(merchandise, "qty", 8, true);
            ValidateUtil.validMustNumber(merchandise, "qty");
            ValidateUtil.validMaxLength(merchandise, "eBayTransactionID", 19, false);
            ValidateUtil.validMaxLength(merchandise, "eBayItemID", 19, false);
        }
    }
}
